﻿using System;
using System.ComponentModel;
using System.Text;
using Eflong.Utility;

namespace Eflong.Monitor
{
    public static class EflongEventId
    {
        public static int Init = 1000;
        public static int SqlError = 2000;
        public static int Error = 3000;
        public static int SaveError = 4000;
        public static int DeleteError = 5000;
    }

    public class EflongMessageShow : IDisposable
    {
        private DateTime tmStart;
        public string msg;

        // Pointer to an external unmanaged resource.
        private IntPtr handle;

        // Other managed resource this class uses.
        private Component component = new Component( );

        // Track whether Dispose has been called.
        private bool disposed = false;

        public StringBuilder sb = new StringBuilder( );

        public EflongMessageShow( string msg )
        {
            tmStart = DateTime.Now;
            this.msg = msg;
            sb.AppendLine( );
            sb.AppendLine( msg );
            sb.Append( "\t开始时刻: " );
            sb.Append( tmStart.ToString( "HH:mm:ss" ) );
            sb.Append( "\t" );
        }

        ~EflongMessageShow( )
        {
            Dispose( false );
        }

        public void Close( )
        {
            Dispose( );
        }

        public void Dispose( )
        {
            Dispose( true );

            GC.SuppressFinalize( this );
        }

        private void Dispose( bool disposing )
        {
            float elapsed = ( ( float )DateTime.Now.Subtract( tmStart ).TotalMilliseconds ) / 1000;

            // msg = sb.ToString();
            sb.AppendLine( );
            sb.Append( "\t" );
            sb.Append( "结束时刻: " );
            sb.Append( DateTime.Now.ToString( "HH:mm:ss" ) );
            sb.Append( " --- " );

            //sb.Append(msg);
            sb.Append( " 消耗时间: " );
            sb.Append( elapsed.ToString( ) );
            sb.AppendLine( );

            LogWrapper.Logger.Debug( sb.ToString( ) );

            if ( !this.disposed )
            {
                if ( disposing )
                {
                    // Release managed resources
                    sb.Remove( 0 , sb.Length );

                    // Dispose managed resources.
                    // component.Dispose();
                }

                // Release unmanaged resources
                // Call the appropriate methods to clean up
                // unmanaged resources here.
                // If disposing is false,
                // only the following code is executed.
                CloseHandle( handle );
                handle = IntPtr.Zero;

                disposed = true;
            }
        }

        // Use interop to call the method necessary
        // to clean up the unmanaged resource.
        [System.Runtime.InteropServices.DllImport( "Kernel32" )]
        private extern static Boolean CloseHandle( IntPtr handle );
    }
}